from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from clapton.apps.photos.models import Photo
from tagging.models import Tag
from tagging.fields import TagField
import clapton.utils.markdown as markdown
import clapton.utils.publisher as publisher

class Weblog(models.Model):
    site = models.ForeignKey(Site, verbose_name=_(u'Site'),) 
    name = models.CharField(_(u'Name'), max_length=250)
    slug = models.SlugField(_(u'Slug'), primary_key='True', unique=True,
                            help_text=_(u'Automatically built from the weblog name.'),)
    description = models.TextField(_(u'Description'), max_length=500,
	                                    blank=True, null=True,
	                                    help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)
    users = models.ManyToManyField(User, verbose_name=_(u'Users'),)

    class Meta:
        verbose_name_plural = _(u'weblogs')	

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return '/blogs/%s/' % (self.slug)

class Entry(models.Model):
    weblog = models.ForeignKey(Weblog, verbose_name=_(u'Blog'),) 
    user = models.ForeignKey(User, verbose_name=_(u'User'),)
    title = models.CharField(_(u'Title'), max_length=250,)
    slug = models.SlugField(_(u'Slug'), unique_for_date='pub_date',
	                    help_text=_(u'Automatically built from the title.'),)
    body = models.TextField(_(u'Entry body'), help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)
    tags = TagField(verbose_name=_(u'Tags'), blank=True, null=True, 
	            help_text=_(u'Separate tags with spaces or commas.'),)
    pub_date = models.DateTimeField(_(u'Date published'), 
	                            default=datetime.now, 
				    help_text=_(u'Publication date of the entry.'),)
    lead_image = models.ForeignKey(Photo, verbose_name=_(u'Lead image'),
                                   blank=True, null=True, 
	                           help_text=_(u'Optional lead image for the entry.'),)
    enable_comments = models.BooleanField(_(u'Comments enabled'), default=True, 
	                                  help_text=_(u'If checked, comments will be allowed for this entry.'),)
    publication_status = models.CharField(_(u'Publication Status'), 
	                                  max_length=32, choices=publisher.PUBLICATION_CHOICES,
					  default='Published',
					  help_text=_(u'Only published entries will appear on the site. Drafts will only be visible on the Administration page.'),)
    
    objects = models.Manager()
    published_objects = publisher.PublishedManager()

    class Meta:
        verbose_name_plural = _(u'entries')
        ordering = ['-pub_date']
        get_latest_by = 'pub_date'

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return '/blogs/%s/%s/%s/' % (self.weblog.slug, 
		             self.pub_date.strftime("%Y/%b/%d").lower(),
				     self.slug)